<?php
namespace app\admin\controller;

use app\admin\model\AccountGroup as AccountGroupModel;
use app\admin\model\Account as AccountModel;
use think\Db;
use think\Request;

class AccountGroup extends Base
{
    public function index()
    {
        $model = new AccountGroupModel;
        $umodel = new AccountModel;

         $model->field('g.id,g.illustration,g.name,g.weight,g.created_at,g.updated_at,a.username,u.countid')
            ->alias('g')
            ->join('(SELECT user_group_id, COUNT(id) as countid from '. $this->dbTablePrefix .'user GROUP BY user_group_id HAVING user_group_id>0) u','g.id = u.user_group_id ','left')
            ->join('admin a','g.admin_id = a.id ','left')
            ->order('g.id','desc');
          //  ->paginate(5);


        //接收ajax传递的值
        $request = request();
        $ispost = $request->isPost();
        if($ispost){
            $id = $this->request->param("editId");
            $edit = AccountGroupModel::field('id,name,illustration,weight,admin_id,created_at,updated_at')->where("id",$id)->find();

            return json([
                'code' => 200,
                'edit'=>$edit,
            ]);
        }

        $result = $model->paginate(10);
        //计算总共记录条数
        $count = $result->total();
        //分页
        $page = $result->render();

        $this->assign('currentPage',$result->currentPage());
        $this->assign('lastPage',$result->lastPage());
        $this->assign('page',$page);
        $this->assign('count',$count);
        $this->assign('accountGroup',$result);
        $this->assign('empty',"暂无数据");
        return $this->fetch();
    }

    public function create()
    {
        //接收ajax传递的值
        if($this->request->isPost()){
            $formdata = $this->request->post();
            $username = Db::query("select id,name from ". $this->dbTablePrefix ."user_group where `name`='".$formdata['name']."' and deleted_at is NULL ");

            if(!empty($username)){
                return json([
                    'code' => 4000,
                    'message'=>"用户名已存在"
                ]);
            }
            $model = new AccountGroupModel;
            $result = $model->allowField(true)->save($formdata);

            if($result == 1){
                return json([
                    'code' => 200,
                ]);
            }

        }
    }

    public function edit($id)
    {
        $model = new AccountGroupModel;
        //接收ajax传递的值
        $ispost = $this->request->isPost();

        if($ispost){
            $formdata = $this->request->post();

            $result = $model->allowField(true)->save($formdata,['id'=>$id]);

            if($result == 1){
                return json([
                    'code' => 200,
                ]);
            }

        }
    }

    //删除单条记录
    public function delete($id)
    {
        $result = AccountGroupModel::update(['deleted_at'=>date('Y-m-d H:i:s')],['id'=>$id]);//软删除

        if($result){
            return json([
                'code' => 200,
            ]);
        }

    }

    //批量删除
    public function deleteArr()
    {
        $ispost = $this->request->isPost();
        if ($ispost) {
            $idArr = $this->request->post();

            $result = AccountGroupModel::where('id','exp','in('.implode(',',$idArr['id']).')')->update(['deleted_at'=>date('Y-m-d H:i:s')]);//软删除

            if($result){
                return json([
                    'code' => 200,
                ]);
            }

        }
    }
}